/* 可以获取openlayers所有的图层; */
function getLayers(map) {
    return map.getLayers().getArray()
}
/* 根据图层的title属性获取对应的图层 */
function getLayerByTitle({
    map,
    title
}) {
    /* 1、将所有的图层获取 */
    let layers = map.getLayers().getArray()
    /* 2、获取title值对应的图层 find */
    let target = layers.find(item => item.get("title") == title)
    return target;
    
}
/* 封装点的样式 */
function setPntStyle({
    radius = 15,
    color = "red"
}) {
    return new ol.style.Style({
        image: new ol.style.Circle({
            radius,
            fill: new ol.style.Fill({
                color
            })
        })
    })
}
/* 封装线的样式 */
function setLineStyle({
    color = "red",
    width = 5
}) {
    return new ol.style.Style({
        stroke: new ol.style.Stroke({
            width,
            color
        })
    })
}
/* 封装区样式 */
function setAreaStyle(color = "#33333390") {
    return new ol.style.Style({
        fill: new ol.style.Fill({
            color: color
        }),
        stroke: new ol.style.Stroke({
            width: 1,
            color: "#ff2d51"
        })
    })
}

function setMarkerStyle(src) {
    const icon = new ol.style.Style({
        /**{olx.style.IconOptions}类型*/
        image: new ol.style.Icon({
            anchor: [0.5, 60],
            anchorOrigin: 'top-right',
            anchorXUnits: 'fraction',
            anchorYUnits: 'pixels',
            offsetOrigin: 'top-right',
            // offset:[0,10],
            //图标缩放比例
            scale: 0.5,
            //透明度
            opacity: 0.75,
            //图标的url
            src
        }),
    })
    return icon
}
/* 
geojson to ol-feature 
业务-复杂业务
技术路线【产品团队】 - 基础设施,产品
*/
function JsonToFeature(data) {
    let format = new ol.format.GeoJSON()
    if (data.features) {
        return format.readFeatures(data)
    } else {
        return format.readFeature(data);
    }
}
/* Feature-GeoJSON */
/* Feature = Array|Object */
function FeatureToJson(feature) {
    let format = new ol.format.GeoJSON()
    if (feature instanceof Array) {
        return format.writeFeaturesObject(feature)
    } else {
        return format.writeFeatureObject(feature)
    }
}
export{
    getLayers,
    getLayerByTitle,
    setPntStyle,
    setLineStyle,
    setAreaStyle,
    setMarkerStyle,
    JsonToFeature,
    FeatureToJson
}